package luccy.image;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import luccy.annotations.Component;

public class Main {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		Bar bar = new Bar();
		Class barClazz = bar.getClass();

		// @Componentがついてなければ、Componentとみなさない
		if (!barClazz.isAnnotationPresent(Component.class)) {
			return;
		}
		for (Method m : barClazz.getDeclaredMethods()) {
			for (Annotation a : m.getDeclaredAnnotations()) {
				Class<?>[] paramTypes = m.getParameterTypes();
				if (paramTypes.length >= 1) {
					Class<?> cl = paramTypes[0];
					Class<? extends Annotation> type = a.annotationType();

					// Methodについている各アノテーションにDummyConfigがついてなければスルー
					if (!type.isAnnotationPresent(DummyConfig.class)) {
						continue;
					}
					// package-infoに定義されていなければスルー
					if (!type.getPackage().isAnnotationPresent(type)) {
						continue;
					}
					for (Method method : type.getDeclaredMethods()) {
						Object invoke = method.invoke(a, new Object[] {});
						if (invoke instanceof Class) {
							Class cz = (Class) invoke;
							if (cl.isAssignableFrom(cz)) {
								Object o = cz.newInstance();

								// injection
								m.invoke(bar, o);
							}
						}
					}
				}
			}
		}
		System.out.println(bar.getS1());
		System.out.println(bar.getS2());
	}
}
